﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using DragDropLib;
using ComIDataObject = System.Runtime.InteropServices.ComTypes.IDataObject;
using DataObject = System.Windows.Forms.DataObject;

namespace AngelPerez.Taringa.PostEdit {
	public partial class ToolboxWindow : Form {
		[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
		private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);

		TreeNode dragNode = null;

		public ToolboxWindow() {
			InitializeComponent();
			SetWindowTheme(treeView1.Handle, "explorer", null);
		}

		private void OnTreeItemDrag(object sender, ItemDragEventArgs e) {
			DoDragDrop(e.Item, DragDropEffects.Copy);

			this.dragNode = (TreeNode)e.Item;
			this.treeView1.SelectedNode = this.dragNode;

			// Reset image list used for drag image
			this.imageListDrag.Images.Clear();
			this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.treeView1.Indent, this.dragNode.Bounds.Height);

			// Create new bitmap
			// This bitmap will contain the tree node image to be dragged
			Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.treeView1.Indent, this.dragNode.Bounds.Height);

			// Get graphics from bitmap
			Graphics gfx = Graphics.FromImage(bmp);

			// Draw node icon into the bitmap
			gfx.DrawImage(this.imageList1.Images[0], 0, 0);

			// Draw node label into bitmap
			gfx.DrawString(this.dragNode.Text,
					this.treeView1.Font,
					new SolidBrush(this.treeView1.ForeColor),
					(float)this.treeView1.Indent, 1.0f);

			// Add bitmap to imagelist
			this.imageListDrag.Images.Add(bmp);

			// Get mouse position in client coordinates
			Point p = this.treeView1.PointToClient(Control.MousePosition);

			// Compute delta between mouse position and node bounds
			int dx = p.X + this.treeView1.Indent - this.dragNode.Bounds.Left;
			int dy = p.Y - this.dragNode.Bounds.Top;

			// Begin dragging image
			if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy)) {
				// Begin dragging
				this.treeView1.DoDragDrop(bmp, DragDropEffects.Move);
				// End dragging image
				DragHelper.ImageList_EndDrag();
			}
		}

		private void treeView1_DragEnter(object sender, DragEventArgs e) {
		
		}

		private void treeView1_DragOver(object sender, DragEventArgs e) {
		}

		private void treeView1_DragLeave(object sender, EventArgs e) {
		}

		private void treeView1_DragDrop(object sender, DragEventArgs e) {
		}

		private void treeView1_MouseDown(object sender, MouseEventArgs e) {
		}
	}
}
